package ee.maoinvest.backend.model.dao.postgresqlimplementations;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ee.maoinvest.backend.database.ConnectionManager;
import ee.maoinvest.backend.model.dao.TempRelationBetweenChildAndParentElectricityMeterDAO;
import ee.maoinvest.backend.model.data.TempRelationBetweenChildAndParentElectricityMeter;

public class TempRelationBetweenChildAndParentElectricityMeterDAOPostgresqlImpl implements TempRelationBetweenChildAndParentElectricityMeterDAO
{
	Logger logger = Logger.getLogger(TempRelationBetweenChildAndParentElectricityMeterDAOPostgresqlImpl.class);
	Connection dbConnection;
	String sql;
	PreparedStatement preparedStatement;
	Statement statement;
	ResultSet resultSet;
	private final int WARNING_NO_ROWS_AFFECTED = 0;
	private int rowsAffected;
	
	public TempRelationBetweenChildAndParentElectricityMeterDAOPostgresqlImpl(Connection dbConnection)
	{
		this.dbConnection = dbConnection;
	}
	
//-----------------START OF BASIC CRUD-----------------------------------

	@Override
	public int insertTempRelationBetweenChildAndParentElectricityMeter(
			TempRelationBetweenChildAndParentElectricityMeter tempRelationBetweenChildAndParentElectricityMeter) 
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "INSERT INTO Temp_relations_between_child_and_parent_electricity_meters " + 
				"(parentElectricityMeterFK, childElectricityMeterFK, " +  
				"dateRelationStarted, dateRelationEnded, regularReadingAtPeriodStartFK, regularReadingAtPeriodEndFK, " +  
				"parentMeterDaytimeReadingAtRelationStartDate, parentMeterNightTimeAtRelationStartDate, parentMeterReactivePowerReadingAtRelationStartDate, " +  
				"childMeterDaytimeReadingAtRelationStartDate, childMeterNightTimeReadingAtRelationStartDate, childMeterReactivePowerReadingAtRelationStartDate, " +  
				"parentMeterDaytimeReadingAtRelationEndDate, parentMeterNightTimeReadingAtRelationEndDate, parentMeterReactivePowerReadingAtRelationEndDate, " + 
				"childMeterDaytimeReadingAtRelationEndDate, childMeterNightTimeReadingAtRelationEndDate, childMeterReactivePowerReadingAtRelationEndDate) " + 
				"VALUES " + 
				"(?, ?, " + 
				"?, ?, ?, ?, " + 
				"?, ?, ?, " + 
				"?, ?, ?, " + 
				"?, ?, ?, " + 
				"?, ?, ?);";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			
			preparedStatement.setInt		(	1, tempRelationBetweenChildAndParentElectricityMeter.getParentElectricityMeterFK());
			preparedStatement.setInt		(	2, tempRelationBetweenChildAndParentElectricityMeter.getChildElectricityMeterFK());
			preparedStatement.setDate		(	3, tempRelationBetweenChildAndParentElectricityMeter.getDateRelationStarted());
			preparedStatement.setObject		(	4, tempRelationBetweenChildAndParentElectricityMeter.getDateRelationEnded(), Types.DATE);
			preparedStatement.setInt		(	5, tempRelationBetweenChildAndParentElectricityMeter.getRegularReadingAtPeriodStartFK());
			preparedStatement.setObject		(	6, tempRelationBetweenChildAndParentElectricityMeter.getRegularReadingAtPeriodEndFK(), Types.INTEGER);
			preparedStatement.setBigDecimal	(	7, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterDaytimeReadingAtRelationStartDate());
			preparedStatement.setBigDecimal	(	8, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterNightTimeAtRelationStartDate());
			preparedStatement.setObject		(	9, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterReactivePowerReadingAtRelationStartDate(), Types.NUMERIC);
			preparedStatement.setBigDecimal	(	10, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterDaytimeReadingAtRelationStartDate());
			preparedStatement.setBigDecimal	(	11, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterNightTimeReadingAtRelationStartDate());
			preparedStatement.setObject		(	12, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterReactivePowerReadingAtRelationStartDate(), Types.NUMERIC);
			preparedStatement.setObject		(	13, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterDaytimeReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	14, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterNightTimeReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	15, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterReactivePowerReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	16, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterDaytimeReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	17, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterNightTimeReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	18, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterReactivePowerReadingAtRelationEndDate(), Types.NUMERIC);
			
			logger.info(preparedStatement.toString());
			rowsAffected = preparedStatement.executeUpdate();
			
			if (rowsAffected == 1)
			{
				resultSet = preparedStatement.getGeneratedKeys();
				resultSet.next(); //answer consist only 1 row, so move the cursor just once to start to process the result
				int newId = resultSet.getInt(1); // 1, because identifiers are held in the first column 
				tempRelationBetweenChildAndParentElectricityMeter.setTempRelationBetweenChildAndParentElectricityMeterID(newId);
				return rowsAffected;
			}
			else
			{
				System.out.println("No rows affected!");
				logger.info("Unsuccessful insertion to a database: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return WARNING_NO_ROWS_AFFECTED;
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}

	@Override
	public TempRelationBetweenChildAndParentElectricityMeter getTempRelationBetweenChildAndParentElectricityMeterByID(
			int tempRelationBetweenChildAndParentElectricityMeterID) 
	{
		resultSet = null;
		TempRelationBetweenChildAndParentElectricityMeter tempRelationBetweenChildAndParentElectricityMeterRetrieved = null;
		sql = "SELECT * FROM Temp_relations_between_child_and_parent_electricity_meters WHERE tempRelationBetweenChildAndParentElectricityMeterID = ?;";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
						sql,
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_READ_ONLY
					);
			//filling the placeholders 
			preparedStatement.setInt(1, tempRelationBetweenChildAndParentElectricityMeterID);
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			//place the cursor at the beginning of a received data set (so it can start to read and process data)
			resultSet.beforeFirst();
			
			if (resultSet.next())
			{
				tempRelationBetweenChildAndParentElectricityMeterRetrieved = fillTempRelationBetweenChildAndParentElectricityMeterWithDataFromDB();
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return tempRelationBetweenChildAndParentElectricityMeterRetrieved;
	}

	@Override
	public int updateTempRelationBetweenChildAndParentElectricityMeter(
			TempRelationBetweenChildAndParentElectricityMeter tempRelationBetweenChildAndParentElectricityMeter) 
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "UPDATE Temp_relations_between_child_and_parent_electricity_meters " + 
				"SET parentElectricityMeterFK = ?, childElectricityMeterFK = ?, " +  
				"dateRelationStarted = ?, dateRelationEnded = ?, regularReadingAtPeriodStartFK = ?, regularReadingAtPeriodEndFK = ?, " +  
				"parentMeterDaytimeReadingAtRelationStartDate = ?, parentMeterNightTimeAtRelationStartDate = ?, parentMeterReactivePowerReadingAtRelationStartDate = ?, " +  
				"childMeterDaytimeReadingAtRelationStartDate = ?, childMeterNightTimeReadingAtRelationStartDate = ?, childMeterReactivePowerReadingAtRelationStartDate = ?, " +  
				"parentMeterDaytimeReadingAtRelationEndDate = ?, parentMeterNightTimeReadingAtRelationEndDate = ?, parentMeterReactivePowerReadingAtRelationEndDate = ?, " + 
				"childMeterDaytimeReadingAtRelationEndDate = ?, childMeterNightTimeReadingAtRelationEndDate = ?, childMeterReactivePowerReadingAtRelationEndDate = ? " + 
				"WHERE tempRelationBetweenChildAndParentElectricityMeterID = ?;";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql);
			
			preparedStatement.setInt		(	1, tempRelationBetweenChildAndParentElectricityMeter.getParentElectricityMeterFK());
			preparedStatement.setInt		(	2, tempRelationBetweenChildAndParentElectricityMeter.getChildElectricityMeterFK());
			preparedStatement.setDate		(	3, tempRelationBetweenChildAndParentElectricityMeter.getDateRelationStarted());
			preparedStatement.setObject		(	4, tempRelationBetweenChildAndParentElectricityMeter.getDateRelationEnded(), Types.DATE);
			preparedStatement.setInt		(	5, tempRelationBetweenChildAndParentElectricityMeter.getRegularReadingAtPeriodStartFK());
			preparedStatement.setObject		(	6, tempRelationBetweenChildAndParentElectricityMeter.getRegularReadingAtPeriodEndFK(), Types.INTEGER);
			preparedStatement.setBigDecimal	(	7, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterDaytimeReadingAtRelationStartDate());
			preparedStatement.setBigDecimal	(	8, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterNightTimeAtRelationStartDate());
			preparedStatement.setObject		(	9, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterReactivePowerReadingAtRelationStartDate(), Types.NUMERIC);
			preparedStatement.setBigDecimal	(	10, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterDaytimeReadingAtRelationStartDate());
			preparedStatement.setBigDecimal	(	11, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterNightTimeReadingAtRelationStartDate());
			preparedStatement.setObject		(	12, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterReactivePowerReadingAtRelationStartDate(), Types.NUMERIC);
			preparedStatement.setObject		(	13, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterDaytimeReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	14, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterNightTimeReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	15, tempRelationBetweenChildAndParentElectricityMeter.getParentMeterReactivePowerReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	16, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterDaytimeReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	17, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterNightTimeReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setObject		(	18, tempRelationBetweenChildAndParentElectricityMeter.getChildMeterReactivePowerReadingAtRelationEndDate(), Types.NUMERIC);
			preparedStatement.setInt		(	19, tempRelationBetweenChildAndParentElectricityMeter.getTempRelationBetweenChildAndParentElectricityMeterID());
			
			rowsAffected = preparedStatement.executeUpdate();
			
			
			if (rowsAffected == 1)
			{
				logger.info("Update sucessful: " + preparedStatement.toString());
				return rowsAffected;
			}
			else
			{
				logger.error("Update was unsuccesful: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
			return WARNING_NO_ROWS_AFFECTED;		
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}

	@Override
	public int deleteTempRelationBetweenChildAndParentElectricityMeter(
			int tempRelationBetweenChildAndParentElectricityMeterID)
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "DELETE FROM Temp_relations_between_child_and_parent_electricity_meters WHERE tempRelationBetweenChildAndParentElectricityMeterID = ?;";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql);
			preparedStatement.setInt(1, tempRelationBetweenChildAndParentElectricityMeterID);
			rowsAffected = preparedStatement.executeUpdate();
			
			
			if (rowsAffected == 1)
			{
				logger.info("Deletion successful: " + preparedStatement.toString());
				return rowsAffected;
			}
			else
			{
				logger.info("Deletion unsuccessful: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch(SQLException e) 
		{
			e.printStackTrace();
			return WARNING_NO_ROWS_AFFECTED;
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}
	
//-----------------END OF BASIC CRUD-----------------------------------
	@Override
	public List<TempRelationBetweenChildAndParentElectricityMeter> getAllTempRelationsBetweenChildAndParentElectricityMeters() 
	{
		List<TempRelationBetweenChildAndParentElectricityMeter> listOfTempRelationBetweenChildAndParentElectricityMeter = 
				new ArrayList<TempRelationBetweenChildAndParentElectricityMeter>();
		TempRelationBetweenChildAndParentElectricityMeter tempRelationBetweenChildAndParentElectricityMeterRetrieved = null;
		sql = "SELECT * FROM Temp_relations_between_child_and_parent_electricity_meters;";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
					sql,
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY
				);
			
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			//place the cursor at the beginning of the result set. Otherwise it can not start reading the result set. 
			resultSet.beforeFirst();
			
			while (resultSet.next())
			{	
				tempRelationBetweenChildAndParentElectricityMeterRetrieved = fillTempRelationBetweenChildAndParentElectricityMeterWithDataFromDB();
			
				listOfTempRelationBetweenChildAndParentElectricityMeter.add(tempRelationBetweenChildAndParentElectricityMeterRetrieved);
			}
		} 
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return listOfTempRelationBetweenChildAndParentElectricityMeter;
	
	}
	
	@Override
	public TempRelationBetweenChildAndParentElectricityMeter getTempRelationBetweenChildAndParentElectricityMeterObjectByChildAndParentMeterIDAndDateRelationEndedIsNull(
			int childElectricityMeterID, int parentElectricityMeterID) 
	{
		resultSet = null;
		TempRelationBetweenChildAndParentElectricityMeter tempRelationBetweenChildAndParentElectricityMeterRetrieved = null;
		sql = "SELECT * FROM Temp_relations_between_child_and_parent_electricity_meters WHERE parentElectricityMeterFk = ? AND childElectricityMeterFK = ? " +
				"AND dateRelationEnded IS NULL";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
						sql,
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_READ_ONLY
					);
			//filling the placeholders 
			preparedStatement.setInt(1, parentElectricityMeterID);
			preparedStatement.setInt(2, childElectricityMeterID);
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			//place the cursor at the beginning of a received data set (so it can start to read and process data)
			resultSet.beforeFirst();
			
			if (resultSet.next())
			{
				tempRelationBetweenChildAndParentElectricityMeterRetrieved = fillTempRelationBetweenChildAndParentElectricityMeterWithDataFromDB();
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return tempRelationBetweenChildAndParentElectricityMeterRetrieved;
	}
	
	@Override
	public List<TempRelationBetweenChildAndParentElectricityMeter> getListOfAllTempRelationBetweenSpecificParentAndItsChildMetersThatExistedBetweenDatesGivenRegularReadingsTookPlace(
			Date minDateRegularReadingWasTaken, Date maxDateRegularReadingWasTaken,
			int specificParentElectricityMeterID)
	{
		List<TempRelationBetweenChildAndParentElectricityMeter> listOfAllTempRelationBetweenSpecificParentAndItsChildMetersThatExistedBetweenDatesGivenRegularReadingsTookPlace = 
				new ArrayList<TempRelationBetweenChildAndParentElectricityMeter>();
		TempRelationBetweenChildAndParentElectricityMeter tempRelationBetweenChildAndParentElectricityMeterRetrieved = null;
	
		sql = "SELECT * FROM Temp_relations_between_child_and_parent_electricity_meters " + 
				"WHERE parentElectricityMeterFK = ? AND ((dateRelationStarted <= ? AND ((dateRelationEnded >= ? AND dateRelationEnded <= ?) OR dateRelationEnded IS NULL)) " + 
				"OR ((dateRelationStarted > ? AND dateRelationStarted <= ?) " + 
				"AND ((dateRelationStarted <= dateRelationEnded AND dateRelationEnded <= ?) OR dateRelationEnded IS NULL))) ORDER BY tempRelationBetweenChildAndParentElectricityMeterID";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
					sql,
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY
				);
			
			preparedStatement.setInt	(1, specificParentElectricityMeterID);
			preparedStatement.setDate	(2, minDateRegularReadingWasTaken);
			preparedStatement.setDate	(3, minDateRegularReadingWasTaken);
			preparedStatement.setDate	(4, maxDateRegularReadingWasTaken);
			preparedStatement.setDate	(5, minDateRegularReadingWasTaken);
			preparedStatement.setDate	(6, maxDateRegularReadingWasTaken);
			preparedStatement.setDate	(7, maxDateRegularReadingWasTaken);
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			//place the cursor at the beginning of the result set. Otherwise it can not start reading the result set. 
			resultSet.beforeFirst();
			
			while (resultSet.next())
			{	
				tempRelationBetweenChildAndParentElectricityMeterRetrieved = fillTempRelationBetweenChildAndParentElectricityMeterWithDataFromDB();
			
				listOfAllTempRelationBetweenSpecificParentAndItsChildMetersThatExistedBetweenDatesGivenRegularReadingsTookPlace.add(tempRelationBetweenChildAndParentElectricityMeterRetrieved);
			}
		} 
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return listOfAllTempRelationBetweenSpecificParentAndItsChildMetersThatExistedBetweenDatesGivenRegularReadingsTookPlace;
	}
	
	//-------------Helper methods-----------------------
	private TempRelationBetweenChildAndParentElectricityMeter fillTempRelationBetweenChildAndParentElectricityMeterWithDataFromDB()
	{
		TempRelationBetweenChildAndParentElectricityMeter tempRelationBetweenChildAndParentElectricityMeterRetrieved = null;
		
		try 
		{
			tempRelationBetweenChildAndParentElectricityMeterRetrieved = new TempRelationBetweenChildAndParentElectricityMeter();
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setTempRelationBetweenChildAndParentElectricityMeterID(resultSet.getInt("tempRelationBetweenChildAndParentElectricityMeterID"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setParentElectricityMeterFK(resultSet.getInt("parentElectricityMeterFK"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setChildElectricityMeterFK(resultSet.getInt("childElectricityMeterFK"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setDateRelationStarted(resultSet.getDate("dateRelationStarted"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setDateRelationEnded((Date) resultSet.getObject("dateRelationEnded"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setRegularReadingAtPeriodStartFK(resultSet.getInt("regularReadingAtPeriodStartFK"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setRegularReadingAtPeriodEndFK((Integer) resultSet.getObject("regularReadingAtPeriodEndFK"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setParentMeterDaytimeReadingAtRelationStartDate(resultSet.getBigDecimal("parentMeterDaytimeReadingAtRelationStartDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setParentMeterNightTimeAtRelationStartDate(resultSet.getBigDecimal("parentMeterNightTimeAtRelationStartDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setParentMeterReactivePowerReadingAtRelationStartDate((BigDecimal) resultSet.getObject("parentMeterReactivePowerReadingAtRelationStartDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setChildMeterDaytimeReadingAtRelationStartDate(resultSet.getBigDecimal("childMeterDaytimeReadingAtRelationStartDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setChildMeterNightTimeReadingAtRelationStartDate(resultSet.getBigDecimal("childMeterNightTimeReadingAtRelationStartDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setChildMeterReactivePowerReadingAtRelationStartDate((BigDecimal) resultSet.getObject("childMeterReactivePowerReadingAtRelationStartDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setParentMeterDaytimeReadingAtRelationEndDate((BigDecimal) resultSet.getObject("parentMeterDaytimeReadingAtRelationEndDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setParentMeterNightTimeReadingAtRelationEndDate((BigDecimal) resultSet.getObject("parentMeterNightTimeReadingAtRelationEndDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setParentMeterReactivePowerReadingAtRelationEndDate((BigDecimal) resultSet.getObject("parentMeterReactivePowerReadingAtRelationEndDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setChildMeterDaytimeReadingAtRelationEndDate((BigDecimal) resultSet.getObject("childMeterDaytimeReadingAtRelationEndDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setChildMeterNightTimeReadingAtRelationEndDate((BigDecimal) resultSet.getObject("childMeterNightTimeReadingAtRelationEndDate"));
			tempRelationBetweenChildAndParentElectricityMeterRetrieved.setChildMeterReactivePowerReadingAtRelationEndDate((BigDecimal) resultSet.getObject("childMeterReactivePowerReadingAtRelationEndDate"));
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		return tempRelationBetweenChildAndParentElectricityMeterRetrieved;
	}
	
}
